var fn = require('../../../util/util.js');
var globalObj = getApp();
Page({
  data: {
    start: 0,
    isFirst: true
  },

  /**
   TODO 页面间传值
   * 第一种：页面跳转，依靠跳转的 url 带参传值，或者通过「页面栈」传值，例：区号的选择
   * 注意：switchTab 导航，不能带参
   * 第二种：本地存取
   * 第三种：将值设置为全局变量，在需要的页面获取
   * 第四种：事件总线
   */

  /**
   * 页面初始化时，由于页面没有生成，所以不能处理和 UI 相关的内容，如：设置置顶栏文字内容 
   * 通过 options 参数，可以获取页面跳转所携带的参数 
   * @param {*} options 
   */
  onLoad: function (options) {
    this.data.category = options.category;
    switch (options.category) {
      case '正在热映': this.data.uType = 'in_theaters'; break;
      case '即将上映': this.data.uType = 'coming_soon'; break;
      case 'top250': this.data.uType = 'top250'; break;
    }
    // 豆瓣电影默认获取前二十条数据
    var url = globalObj.globalData.m_url + '/v2/movie/' + this.data.uType;
    this.data.nUrl = url;
    fn.http(url, this.getInTheatersMovies, 'movies', this.data.uType);

    // wx.startPullDownRefresh();
  },
  onReady: function () {
    wx.setNavigationBarTitle({
      title: this.data.category,
    });
  },
  // 页面下拉刷新时，小程序框架会调用该函数
  onPullDownRefresh:function(){
    // var that=this;
    // wx.startPullDownRefresh({
    //   success:function(){
    //     var url = that.data.nUrl + '?start=0&count=20';
    //     fn.http(url, that.getInTheatersMovies, 'movies', that.data.uType);
    //     console.log(1);
    //   },
    //   fail:function(){
    //     console.log(0);
    //   }
    // })
    
  },
  scrollToLower: function () {
    // 展示加载动画
    wx.showNavigationBarLoading();
    var url = this.data.nUrl + '?start=' + this.data.start + '&count=20';
    fn.http(url, this.getInTheatersMovies, 'movies', this.data.uType);
  },

  /**
   * 页面上拉触底事件的处理函数
   * 做分页处理
   */
  onReachBottom:function(){
    // 展示加载动画
    wx.showNavigationBarLoading();
    var url = this.data.nUrl + '?start=' + this.data.start + '&count=20';
    fn.http(url, this.getInTheatersMovies, 'movies', this.data.uType);
  },

  getInTheatersMovies: function (data, key, name) {
    var infoArr = data.subjects,
      moviesData = [];
    for (var i = 0; i < infoArr.length; i++) {
      var obj = {};
      obj['id'] = infoArr[i]['id'];
      obj['img'] = infoArr[i]['images']['large'];
      obj['title'] = infoArr[i]['title'].length > 6 ? infoArr[i]['title'].substr(0, 6) + '...' : infoArr[i]['title'];
      obj['score'] = infoArr[i]['rating']['average'];
      obj['star'] = fn.getStarArr(infoArr[i]['rating']['stars']);
      moviesData.push(obj);
    }    
    var temp = {};
    if (this.data.isFirst) {
      temp[key] = { 'movieType': name, 'movieList': moviesData };
      this.data.isFirst = false;
    } else {
      // 只能用“[]”，不能用“.”
      var oldArr = this.data[key]['movieList'].concat(moviesData);
      temp[key] = { 'movieType': name, 'movieList': oldArr };
      
    }
    this.setData(temp);
    this.data.start += 20;
    wx.hideNavigationBarLoading();
  },
  onOpenMovieDetal: function (even) {
    var id = even.currentTarget.dataset.movieId;
    wx.navigateTo({
      url: '/page/movies/movie-detail/movie-detail?id=' + id,
    });
  },

})